﻿using System;
//using HundredMilesSoftware.UltraID3Lib;
using TagLib;


namespace JipPlayer_2._0
{
    public partial class frmID3TagEditor : DevExpress.XtraEditors.XtraForm
    {
        public frmID3TagEditor()
        {
            InitializeComponent();
        }

        
        public string url { get; set; }
        File ID3;

        private void ID3TagEditor_Load(object sender, EventArgs e)
        {
            Text = "ID3 Tag Editor - " + System.IO.Path.GetFileName(url);
            GetID3(url);
        }

        private void GetID3(string url)
        {
            ID3 = File.Create(url);
            //  ID3 Reader            
            if ((url.EndsWith(".mp3")))
            {
                txtAlbum.Text = ID3.Tag.Album;
                txtTrack.Text = ID3.Tag.Title;
                txtArtist.Text = ID3.Tag.FirstPerformer;
                txtNumber.Text = ID3.Tag.Track.ToString();
                txtYear.Text = ID3.Tag.Year.ToString();
                txtComments.Text = ID3.Tag.Comment;
                txtGenre.Text = ID3.Tag.FirstGenre;
            }
        }

        private void btnSaveClose_Click(object sender, EventArgs e)
        {
            try
            {
                string[] Artists = new string[] {txtArtist.Text} ;
                string[] Genres = new string[] {txtGenre.Text};                
                ID3 = File.Create(url);
                ID3.Tag.Clear();
                ID3.Tag.Title = txtTrack.Text;
                ID3.Tag.Album = txtAlbum.Text;
                ID3.Tag.Performers = Artists;
                ID3.Tag.Genres = Genres;
                ID3.Tag.Comment = txtComments.Text;
                if (txtNumber.Text != "")
                {
                    ID3.Tag.Track = Convert.ToUInt16(txtNumber.Text);
                }
                if (txtYear.Text != "")
                {
                    ID3.Tag.Year = Convert.ToUInt16(txtYear.Text);
                }
                ID3.Save();
            }
            catch(Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }



    }
}